package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BeanHelper;
import com.usian.common.util.JwtUtils;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.dtos.WmUserLoginDto;
import com.usian.wemedia.entity.WmUser;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-05
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    @Override
    public WmUserDto fingByApUserId(Integer apUserId) {
        //构造查询条件
        QueryWrapper<WmUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getApUserId,apUserId);
        //查询信息
        WmUser wmUser = getOne(queryWrapper);
        //对象转换
        if (wmUser!=null){
            WmUserDto wmUserDto = BeanHelper.copyProperties(wmUser,WmUserDto.class);
            return wmUserDto;
        }
        return null;
    }

    @Override
    public Integer saveWmUser(WmUserDto wmUserDto) {
            WmUser wmUser=BeanHelper.copyProperties(wmUserDto,WmUser.class);
            wmUser.setStatus(9);//正常可用状态
            wmUser.setCreatedTime(new Date());
            wmUser.setType(0);//个人用户名
        boolean save = save(wmUser);
        if (!save){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return  wmUser.getId();

    }

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public Map<String, Object> login(WmUserLoginDto dto) {
        //1、参数校验
        if (dto==null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2、基于用户名查询用户信息
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        WmUser wmUser = getOne(queryWrapper);
        if (wmUser==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"用户名错误");
        }
        //3、比较密码是否正确
        boolean b = passwordEncoder.matches(dto.getPassword(), wmUser.getPassword());
        if (!b){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"密码错误");
        }
        //4、基于Jwt工具类生成token
        String token = JwtUtils.generateTokenExpireInSeconds(wmUser.getId(), 3600);
        //5、组装map集合数据
        Map<String,Object> map = new HashMap<>();
        wmUser.setPassword("");
        wmUser.setEmail("");
        map.put("user",wmUser);
        map.put("token",token);
        return map;

    }
}
